package com.sunshine.model;

import com.sunshine.bean.Information;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by along on 11/18/17.
 */
@Mapper
public interface InformationMapper {

    @Insert("INSERT INTO informations(id, name, contact,title, content, type,publical,location,attachment,status,created_date,conductor,response_date,response) "
            + " VALUES(#{id}, #{name},#{contact}, #{title}, #{content}, #{type},#{publical},#{location},#{attachment},#{status},now(),#{conductor},#{responseDate},#{response})")
    int insert(Information information);

    @Update("UPDATE informations SET status = #{status}, conductor = #{conductor},response = #{response},response_date = now() WHERE WHERE id = #{id}")
    int update(Information information);

    @Select("SELECT * FROM informations WHERE id = #{id} LIMIT 1")
    @Results(id = "InformationMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "contact", column = "contact"),
            @Result(property = "title", column = "title"),
            @Result(property = "content", column = "content"),
            @Result(property = "type", column = "type"),
            @Result(property = "publical", column = "publical"),
            @Result(property = "location", column = "location"),
            @Result(property = "attachment", column = "attachment"),
            @Result(property = "status", column = "status"),
            @Result(property = "createdDate", column = "created_date"),
            @Result(property = "conductor", column = "conductor"),
            @Result(property = "responseDate", column = "responseDate"),
            @Result(property = "response", column = "response")
    })
    Information findById(String id);

    @Update("UPDATE informations SET status = #{status},WHERE WHERE id = #{id}")
    int updateStatusById(@Param(value = "id")String id,@Param(value = "status")String status);

    @Select("SELECT * FROM informations WHERE publical = 1 LIMIT 100")
    @ResultMap("InformationMap")
    List<Information> findPublic();

    @Select("SELECT * FROM informations WHERE type = #{type}")
    @ResultMap("InformationMap")
    List<Information> findByType(@Param(value = "type")String type);

    @Select("SELECT * FROM informations WHERE type = #{type} LIMIT #{n}")
    @ResultMap("InformationMap")
    List<Information> previewByType(@Param(value = "type")String type,@Param(value = "n")int n);

    @Select("SELECT * FROM informations WHERE status = #{status}")
    @ResultMap("InformationMap")
    List<Information> findByStatus(@Param(value = "status")String status);

    @Select("SELECT COUNT(*) FROM informations")
    int countSum();

    @Select("SELECT COUNT(*) FROM informations WHERE status = 'END'")
    int countEnd();
}
